package com.simon.hse.activity.danger;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonAdapter;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.entity.DangerAddLevelOutput;
import com.simon.hse.http.entity.DangerEliminateOutput;
import com.simon.hse.http.entity.DropDownOutput;
import com.simon.hse.utils.PopupWindowUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 隐患排查任务详情
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_danger_eliminate_detail)
public class DangerEliminateDetailActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.tv_danger_eliminate_detail_title)
    TextView mTv_danger_eliminate_detail_title;

    @ViewInject(R.id.tv_danger_eliminate_detail_name)
    TextView mTv_danger_eliminate_detail_name;

    @ViewInject(R.id.tv_danger_eliminate_detail_company)
    TextView mTv_danger_eliminate_detail_company;

    @ViewInject(R.id.tv_danger_eliminate_detail_purpose)
    TextView mTv_danger_eliminate_detail_purpose;

    @ViewInject(R.id.tv_danger_eliminate_detail_person)
    TextView mTv_danger_eliminate_detail_person;

    @ViewInject(R.id.tv_danger_eliminate_detail_require)
    TextView mTv_danger_eliminate_detail_require;

    @ViewInject(R.id.tv_danger_eliminate_detail_content)
    TextView mTv_danger_eliminate_detail_content;

    @ViewInject(R.id.tv_danger_eliminate_detail_type)
    TextView mTv_danger_eliminate_detail_type;

    @ViewInject(R.id.tv_danger_eliminate_detail_station)
    TextView mTv_danger_eliminate_detail_station;

    @ViewInject(R.id.tv_danger_eliminate_detail_area)
    TextView mTv_danger_eliminate_detail_area;

    @ViewInject(R.id.et_danger_eliminate_detail_project)
    EditText mEt_danger_eliminate_detail_project;

    @ViewInject(R.id.et_danger_eliminate_detail_introduce)
    EditText mEt_danger_eliminate_detail_introduce;

    @ViewInject(R.id.et_danger_eliminate_detail_remark)
    EditText mEt_danger_eliminate_detail_remark;

    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;

    private CommonAdapter mCommonAdapter;
    private List<DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean.YHScreeningPlanProDetailBean> mDataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("隐患排查任务详情");

        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left, R.id.et_danger_eliminate_detail_add, R.id.tv_danger_eliminate_detail_confirm})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.et_danger_eliminate_detail_add:
                Intent intent = new Intent(DangerEliminateDetailActivity.this, DangerEliminateLevelActivity.class);
                startActivityForResult(intent, 0);
                break;
            case R.id.tv_danger_eliminate_detail_confirm:

                break;
        }
    }

    private void initRecyclerView() {
        LinearLayoutManager llm = new LinearLayoutManager(DangerEliminateDetailActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonAdapter = new CommonAdapter(DangerEliminateDetailActivity.this, R.layout.layout_item_danger_eliminate_detail, mDataBeanList);
        mCommonAdapter.setItemDataListener(new CommonAdapter.ItemDataListener<DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean.YHScreeningPlanProDetailBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean.YHScreeningPlanProDetailBean dataBean) {
                TextView tv_standard_eliminate_content = holder.getView(R.id.tv_standard_eliminate_content);
                TextView tv_standard_eliminate_level = holder.getView(R.id.tv_standard_eliminate_level);
                final LinearLayout ll_standard_eliminate_state = holder.getView(R.id.ll_standard_eliminate_state);
                final TextView tv_standard_eliminate_state = holder.getView(R.id.tv_standard_eliminate_state);

                tv_standard_eliminate_content.setText(dataBean.getStandardContent());
                tv_standard_eliminate_level.setText(dataBean.getRiskLevelText());
                tv_standard_eliminate_state.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        initConditionPop(ll_standard_eliminate_state, tv_standard_eliminate_state, MyApplication.getInstance().mDangerConditionsList);
                    }
                });
            }
        });

        mCommonAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {

            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonAdapter);
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle == null)
            return;
        DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean projectDetails = (DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean) bundle.getSerializable("THIRD_LEVEL");

        mTv_danger_eliminate_detail_title.setText(projectDetails.getProject());
        mTv_danger_eliminate_detail_name.setText(projectDetails.getPersonInChargeText());
        String unit = projectDetails.getUnit();
        for (DropDownOutput.DataBean.DataDicListBean.ValueBean valueBean : MyApplication.getInstance().mDangerDepartList) {
            if (valueBean.getKey().equals(unit)) {
                mTv_danger_eliminate_detail_company.setText(valueBean.getValue());
            }
        }
        mTv_danger_eliminate_detail_purpose.setText(projectDetails.getObjective());
        List<DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean.YHScreeningPlanUserBean> yh_screeningPlanUser = projectDetails.getYH_ScreeningPlanUser();
        StringBuffer sbUser = new StringBuffer();
        for (DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean.YHScreeningPlanUserBean yhScreeningPlanUserBean : yh_screeningPlanUser) {
            sbUser.append(yhScreeningPlanUserBean.getUserText() + "；");
        }
        mTv_danger_eliminate_detail_person.setText(sbUser.toString());
        mTv_danger_eliminate_detail_require.setText(projectDetails.getRequirements());
        mTv_danger_eliminate_detail_content.setText(projectDetails.getContent());
        mTv_danger_eliminate_detail_type.setText(projectDetails.getStandardType());
        mTv_danger_eliminate_detail_station.setText(projectDetails.getInvestigationPost());

        mTv_danger_eliminate_detail_area.setText(projectDetails.getRegionText());
        mEt_danger_eliminate_detail_introduce.setText(projectDetails.getRemark());
        mEt_danger_eliminate_detail_remark.setText(projectDetails.getRemark());
        mEt_danger_eliminate_detail_project.setText(projectDetails.getProject());

        List<DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean.YHScreeningPlanProDetailBean> proDetailBeanList = projectDetails.getYH_ScreeningPlanProDetail();
        mDataBeanList.addAll(proDetailBeanList);
        mCommonAdapter.notifyDataSetChanged();
    }

    /**
     * 基本信息Pop
     */
    private void initConditionPop(LinearLayout mLl, final TextView mTv, final List<DropDownOutput.DataBean.DataDicListBean.ValueBean> mList) {
        View inflate = LayoutInflater.from(DangerEliminateDetailActivity.this).inflate(R.layout.layout_common_more_department, null);
        RecyclerView mRecyclerView = (RecyclerView) inflate.findViewById(R.id.common_recyclerview);
        LinearLayoutManager llm = new LinearLayoutManager(DangerEliminateDetailActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(DangerEliminateDetailActivity.this, R.layout.layout_item_common_more_department, mList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<DropDownOutput.DataBean.DataDicListBean.ValueBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, DropDownOutput.DataBean.DataDicListBean.ValueBean dataBean) {
                TextView tv_department = holder.getView(R.id.tv_department);
                tv_department.setText(dataBean.getValue());
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                DropDownOutput.DataBean.DataDicListBean.ValueBean valueBean = mList.get(position);
                mTv.setText(valueBean.getValue());
                PopupWindowUtils.getInstance().closePop();
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
        PopupWindowUtils.getInstance().createScalePopupWindow(DangerEliminateDetailActivity.this, inflate, mLl);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        Bundle bundle = data.getExtras();
        if (bundle == null)
            return;
        Log.i("Simon", "result = " + mDataBeanList.size());

        DangerAddLevelOutput.DataBean.PageDataBean pageDataBean = (DangerAddLevelOutput.DataBean.PageDataBean) bundle.getSerializable("DANGER_LEVEL");
        DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean.YHScreeningPlanProDetailBean planProDetailBean = new DangerEliminateOutput.DataBean.PageDataBean.YHScreeningPlanProjectBean.ScreeningPlanProjectDetailsBean.YHScreeningPlanProDetailBean();
        planProDetailBean.setStandardContent(pageDataBean.getInvestigationContents());
        planProDetailBean.setRiskLevelText(pageDataBean.getRiskLevelText());
        mDataBeanList.add(planProDetailBean);
        Log.i("Simon", "result = " + mDataBeanList.size());
        mCommonAdapter.notifyDataSetChanged();
    }
}
